#!/usr/bin/env python
# file pyvest.py
import datetime
import os.path
import data
from pysqlite2 import dbapi2 as sqlite
import pylab

__version__ = "$Revision: 1.10 $"
__author__ = "Ramesh Balasubramanian <ramesh@finpy.org>"


def compoundInterest(presentValue, 
                     periodicRate, 
                     periodsCount):
    """
    -----------------------------------------------------------------------
    Function: compoundInterest(presentValue, periodicRate, periodsCount)
    Compounding is based on the priciple of earning interest-on-interest. 
    Components of compound interest calculation:
        - Principal (a.k.a Present Value) - original amount being invested.
        - Rate of interest - interest earned per period
        - Time Periods: number of periods over which interest is earned.
    Calculation: 
        - Future Value = PresentValue * ((1 + Interest) ** TimePeriods)
    -----------------------------------------------------------------------
    Parameters: 
        - presentValue, > 0
        - periodicRate, > 0, expressed as a percentage value
        - periodsCount, >=0
    -----------------------------------------------------------------------
    Functionality:
        - Computes future value for a number of time periods.
        - Plots a graph with time periods on x-axis and value on y-axis
    -----------------------------------------------------------------------
    Sample Usage:
    -----------------------------------------------------------------------
    import pyvest
    ci = pyvest.compoundInterest(presentValue = 30000, 
                                 periodicRate = 6.5, 
                                 periodsCount = 20)
    print ci
    -----------------------------------------------------------------------
    """
    assert periodsCount > 0, "Number of periods must be > 0"
    assert presentValue > 0, "PV must be > 0"
    assert periodicRate >= 0, "Interest Rate must be >= 0"
    futureValues = \
        [round(presentValue * (1.0 + (periodicRate / 100.0)) ** period) \
        for period in range(periodsCount + 1)]
    pylab.xlabel('Time Periods')
    pylab.ylabel('Future Values')
    pylab.title('Compound Interest ' + '%.2f' % (periodicRate) + '%')
    assert futureValues != None
    assert len(futureValues[1:]) == len(range(periodsCount))
    pylab.bar(range(periodsCount), futureValues[1:], width = 1)
    limits = \
        [0, periodsCount+0.5, futureValues[0], (1.5 * futureValues[-1]) - \
         (0.5 * futureValues[-2])]
    pylab.axis(limits)
    pylab.yticks(futureValues, ['%d' % fv for fv in futureValues])
    pylab.show()
    return futureValues

def bondMeasures(price,                # clean price
                 maturityDate,         # datetime.date datatype
                 settlementDate,       # datetime.date datatype
                 coupon = 0,           # percentage
                 principal = 1000,     # principal paid, if held to maturity
                 couponsPerYear = 2):  # number of coupons per year
    """
    -----------------------------------------------------------------------
    Function bondMeasures(price, maturityDate, settlementDate, 
                          coupon = 0, principal = 1000, couponsPerYear = 2)
    Returns the following as a tuple:
    1. accreued interest
    2. current yield
    3. ytm
    4. amount and timing of cash flows
    5. reinvestment income to be earned to realize ytm
    -----------------------------------------------------------------------
    """
    # from maturityDate compute the last coupon date
    # compute accrued interest from last coupon date and settlement date
    

if __name__ == '__main__':
	pass
